package com.example.superoa.model.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.dao.ActivityDao;
import com.example.superoa.model.dao.ApplyDao;
import com.example.superoa.model.dao.ApprovalDao;
import com.example.superoa.model.dao.CourseDao;
import com.example.superoa.model.dao.RecordDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.dao.TeacherDao;
import com.example.superoa.model.dao.TtestDao;
import com.example.superoa.model.mappers.TrainMapper;
import com.example.superoa.model.pojos.Activity;
import com.example.superoa.model.pojos.Apply;
import com.example.superoa.model.pojos.Approval;
import com.example.superoa.model.pojos.Course;
import com.example.superoa.model.pojos.Record;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.pojos.Teacher;
import com.example.superoa.model.pojos.Ttest;

@Service
@Transactional(rollbackFor=Exception.class)
public class TrainService {
	@Autowired
	private TrainMapper tm;
	@Autowired
	private CourseDao cd;
	@Autowired
	private ActivityDao ad;
	@Autowired
	private TtestDao td;
	@Autowired
	private TeacherDao tcd;
	@Autowired
	private StaffDao sd;
	@Autowired
	private RecordDao rd;
	@Autowired
	private ApplyDao appd;
	@Autowired
	private ApprovalDao ard;
	//新增课程
		
	public void addcourse(int teacher) {
		
	Course cc=cd.findById(bb()).get();
	Teacher teach=tcd.findById(teacher).get();
	List<Course> tc=teach.getCourses();
	if(tc==null) {
		tc=new ArrayList<>();
	}
	tc.add(cc);
	teach.setCourses(tc);
	shenqings(bb());
	}
	public void aa(Course course) {
		cd.save(course);
	}
	public int bb() {
		return tm.selectLimitOne();
	}
	//新增培训活动1
	public void addActone(Activity act,int course_id,Date date) {
		Course c=cd.findById(course_id).get();
		act.setActivityTime(date);
		act.setActStatic(1);
		act.setCourse(c);
		ad.save(act);
	}
	//2
	public void addActtwo(List<Integer> ids) {
		for (Integer i : ids) {
			tm.insertActivity(act(), i);
		}
	}
	//3
	public int act() {
		return tm.selectActOne();
	}
	//禁用课程
	public void deletecourse(int course_id) {
		Course cou = cd.findById(course_id).get();
		cd.delete(cou);
		
	}
	
	//查询所有讲师信息
	public JSONArray findTeacherByName() {
			return tm.findTeacherByName();
	}
	
	//查询课程库
	public JSONArray findCourseku() {	
			return tm.findCourseku();	
	}
	
	//查询所有试卷
	public JSONArray findShijuan() {
		return tm.findShijuan();
	}
	//显示培训课程记录
	public JSONArray findCourse() {
		return tm.findCourse();
	}
	
	//显示讲师表
	public JSONArray findTeachers() {
		return tm.findTeachers();
	}
	//显示讲师申请记录
	public JSONArray findTeacher() {
		return tm.findTeacher();
	}
	//培训记录
	public JSONArray findRecord() {
		return tm.findRecord();
	}
	//删除培训记录
	public void deleteRecord(int record_id) {
		Record r= rd.findById(record_id).get();
		rd.delete(r);
	}
	//根据培训活动id查询该培训活动下员工的考试状态
	public JSONArray findActStaff(int activity_id) {
			return tm.findActStaff(activity_id);
	}
	public JSONArray findCoursekuBytype() {
		return tm.findCoursekuBytype();
	}
	public JSONArray findCoursekuById(int course_id) {
		return tm.findCoursekuById(course_id);
	}
	//编辑课程库数据初始化
	public JSONArray sjcsh(int course_id) {
		return tm.sjcsh(course_id);
	}
	/*//课程库编辑
	public void bjCourse(int course_id,int ttestid,int teacher_id) {
		Course cou = cd.findById(course_id).get();
		cou.setCourseName(cou.getCourseName());
		cou.setCourseType(cou.getCourseType());
		cou.setCourseDetails(cou.getCourseDetails());		
		//课程表和试卷   多对1   试卷是主控方
		 Ttest test = td.findById(ttestid).get();
		 cou.setTtest(test);
		 cd.save(cou);	
		 //修改课程和讲师的中间表
		 tm.xiugai(teacher_id, course_id);
	}*/
	//删除
	public void sccourse(int activity_id) {
		Activity act = ad.findById(activity_id).get();
		tm.shanchuact(activity_id);
		ad.delete(act);
	}
	//显示培训通知信息
	public JSONArray noticefind() {
		return tm.noticefind();
	}
	//培训通知的查看
	public JSONArray findntchakan(int activity_id) {
		return tm.findntchakan(activity_id);
	}
	//培训通知的表格
	public JSONArray findnttable(int activity_id) {
		return tm.findnttable(activity_id);
	}
	//培训记录表格
	public JSONArray findrecords() {
		return tm.findrecords();
	}
	//薪资培训记录
	//删除
	public void delectre(int record_id) {
		Record re = rd.findById(record_id).get();
		rd.delete(re);
	}
	//教师管理表格
	public JSONArray findtea() {
		return tm.findtea();
	}
	//讲师申请
	public JSONArray findapp() {
		return tm.findapp();
	}
	//新增培训申请
	public void addapplys(int staff_id,String applyreason,String applytype,Integer staffid) {
		//新增申请表
		Apply app=new Apply();		
		int apply_id=tm.apply_id()+1;
		app.setApplyId(apply_id);
		app.setApplyReason(applyreason);
		app.setApplyTime(new Date());
		app.setApplyType(applytype);
		Staff sta = sd.findById(staff_id).get();
		app.setStaff(sta);			
		appd.save(app);
		//新增审核表
		Approval ar=new Approval();
		Apply a_id = appd.findById(apply_id).get();
		ar.setApply(a_id);
		ar.setPxapprovalState("1");		
		ar.setStaff(sd.findById(staffid).get());
		ard.save(ar);
	}
	//讲师申请撤回
	public void delectApply(int apply_id) {
		Apply app = appd.findById(apply_id).get();
		JSONArray json = tm.finddeleteapp(apply_id);
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			String statc=(String) obj.get("pxapproval_state");
			if(statc.equals("1")) {
				for (Approval entity : app.getApprovals()) {
					ard.deleteById(entity.getPxapprovalId());
				}
			}
		}		
		appd.deleteById(apply_id);
	}
	//讲师申请删除
	public void delectteas(int apply_id) {
		Apply app = appd.findById(apply_id).get();
		for (Approval entity : app.getApprovals()) {
			ard.deleteById(entity.getPxapprovalId());
		}	
			appd.deleteById(apply_id);
	}
	//删除讲师
	public void delecttea(int teacher_id) {
		tcd.deleteById(teacher_id);
	}
	//删除活动课程中的培训人员
	public void deletetac(int  activity_id,int staff_id) {
		tm.deleteactstaff(activity_id,staff_id);
	}
	//讲师送审
	public void songshen(int app_id) {
		ard.findById(app_id).get();
	}
	//新增培训记录
	public void addred() {
		Record re=new Record();
	}
	//新增培训记录
	public void jiluadd(int activity_id,int course_id) {
		Record re=new Record();
		Activity act = ad.findById(activity_id).get();
		re.setActivity(act);
		Course cou = cd.findById(course_id).get();
		re.setCourse(cou);
		re.setStaff(null);
		re.setRecordState("1");
		re.setTtest(null);
		re.setXuefen(0);
		rd.save(re);
	}
	public JSONArray findrecordact() {
		return tm.findrecordact();
	}
	public JSONArray applyxq(int apply_id) {
		return tm.applyxq(apply_id);
	}
	//发起申请
	public void shenqings(int course_id) {
		Apply app=new Apply();	
		Course cou = cd.findById(course_id).get();
		app.setCourse(cou);		
		appd.save(app);
		
		//新增审核表
		Approval ar=new Approval();
		Apply a_id = appd.findById(app.getApplyId()).get();
		ar.setApply(a_id);
		ar.setPxapprovalState("1");		
		ard.save(ar);		
	}
	public void shenqing(Integer staff_id,Integer staffid,String reason,String type,int apply_id,int ard_id) {
		Apply app=appd.findById(apply_id).get();	
		app.setApplyReason(reason);
		app.setApplyTime(new Date());
		app.setApplyType(type);
		Staff sta = sd.findById(staff_id).get();
		app.setStaff(sta);			
		appd.save(app);
		
		//新增审核表
		Approval ar=ard.findById(ard_id).get();	
		ar.setStaff(sd.findById(staffid).get());
		ard.save(ar);		
	}
	public JSONArray noticestaff(int staff_id,Date shijian) {
		return tm.noticestaff(staff_id, shijian);
	}
	public JSONObject depart_ids(int staff_id) {
		return tm.departsid(staff_id);
	}
}













